# AFR Website Build Script (Windows PowerShell)
Write-Host "Starting AFR Website Docker build..." -ForegroundColor Green

# Set variables
$IMAGE_NAME = "afrwebsite"
$TAG = "latest"
$OUTPUT_FILE = "afrwebsite.tar"

# Check if Docker is running
Write-Host "Checking Docker status..." -ForegroundColor Yellow
try {
    docker version | Out-Null
    Write-Host "Docker is running" -ForegroundColor Green
} catch {
    Write-Host "Docker is not running, please start Docker Desktop" -ForegroundColor Red
    exit 1
}

# Stop and remove old container
Write-Host "Cleaning up old containers..." -ForegroundColor Yellow
docker stop afrwebsite-container 2>$null
docker rm afrwebsite-container 2>$null

# Remove old image
Write-Host "Cleaning up old images..." -ForegroundColor Yellow
docker rmi ${IMAGE_NAME}:${TAG} 2>$null

# Build new image
Write-Host "Building Docker image..." -ForegroundColor Yellow
docker build -t ${IMAGE_NAME}:${TAG} .

# Check if build was successful
if ($LASTEXITCODE -eq 0) {
    Write-Host "Image build successful!" -ForegroundColor Green
} else {
    Write-Host "Image build failed!" -ForegroundColor Red
    exit 1
}

# Export image
Write-Host "Exporting image file..." -ForegroundColor Yellow
docker save -o $OUTPUT_FILE ${IMAGE_NAME}:${TAG}

if ($LASTEXITCODE -eq 0) {
    Write-Host "Image export successful!" -ForegroundColor Green
} else {
    Write-Host "Image export failed!" -ForegroundColor Red
    exit 1
}

# Show file information
Write-Host "File information:" -ForegroundColor Cyan
$size = (Get-Item $OUTPUT_FILE).Length
$sizeMB = [math]::Round($size / 1MB, 2)
Write-Host "Image file: $OUTPUT_FILE ($sizeMB MB)" -ForegroundColor Cyan

# Show image information
Write-Host "Image information:" -ForegroundColor Cyan
docker images | Select-String $IMAGE_NAME

Write-Host "Build completed!" -ForegroundColor Green 